import { Prisma } from "@prisma/client";

export class PaginationUtil {
  /**
   * 通用分页查询
   * @param model Prisma
   */
  static async paginate<T>(
    model: any,
    options: Prisma.UserFindManyArgs,
    page: number = 1,
    pageSize: number = 10
  ) {
    const skip = (page - 1) * pageSize;
    const [data, total] = await Promise.all([
      model.findMany({
        // 分页
        skip,
        take: pageSize,
        ...options,
      }),
      model.count({ where: options.where }), // 计算所有条目
    ]);
    return {
      total,
      page,
      pageSize,
      data,
    };
  }
}
